package com.supermarket.entity.DTO;

import com.supermarket.entity.Address;
import com.supermarket.entity.OrderDelivery;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单配送DTO，包含订单配送信息和订单详情
 */
@Data
public class OrderDeliveryDTO {
    private static final long serialVersionUID = 1L;

    /**
     * 配送ID
     */
    private Integer id;

    /**
     * 订单ID
     */
    private Integer orderId;

    /**
     * 骑手ID
     */
    private Integer riderId;

    /**
     * 送达地址
     */
    private String deliveryAddress;

    /**
     * 取件时间
     */
    private Date pickupTime;

    /**
     * 预计送达时间
     */
    private Date estimatedDeliveryTime;

    /**
     * 实际送达时间
     */
    private Date actualDeliveryTime;

    /**
     * 配送状态：PENDING-待分配，ASSIGNED-已分配，PICKED-已取件，DELIVERED-已送达
     */
    private String deliveryStatus;

    /**
     * 配送备注
     */
    private String deliveryNote;

    /**
     * 骑手评分（1-5分）
     */
    private Integer riderRating;

    /**
     * 骑手评价
     */
    private String riderComment;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 订单号
     */
    private String orderNo;

    /**
     * 订单总金额
     */
    private BigDecimal totalAmount;

    /**
     * 实际支付金额
     */
    private BigDecimal actualAmount;

    /**
     * 订单状态
     */
    private String orderStatus;

    /**
     * 支付方式
     */
    private String paymentType;

    /**
     * 下单用户
     */
    private String nickname;

    /**
     * 用户联系电话
     */
    private String phone;
    
    /**
     * 地址信息
     */
    private Address addressInfo;

    /**
     * 订单项数量
     */
    private Integer itemCount;

    /**
     * 订单商品简要信息
     */
    private List<String> itemNames;

    /**
     * 从OrderDelivery和OrderDTO创建OrderDeliveryDTO的方法
     */
    public static OrderDeliveryDTO fromEntities(OrderDelivery delivery, OrderDTO order) {
        OrderDeliveryDTO dto = new OrderDeliveryDTO();
        
        // 复制OrderDelivery的属性
        dto.setId(delivery.getId());
        dto.setOrderId(delivery.getOrderId());
        dto.setRiderId(delivery.getRiderId());
        dto.setPickupTime(delivery.getPickupTime());
        dto.setEstimatedDeliveryTime(delivery.getEstimatedDeliveryTime());
        dto.setActualDeliveryTime(delivery.getActualDeliveryTime());
        dto.setDeliveryStatus(delivery.getDeliveryStatus());
        dto.setDeliveryNote(delivery.getDeliveryNote());
        dto.setRiderRating(delivery.getRiderRating());
        dto.setRiderComment(delivery.getRiderComment());
        dto.setCreateTime(delivery.getCreateTime());
        dto.setUpdateTime(delivery.getUpdateTime());
        
        // 设置OrderDTO的属性
        if (order != null) {
            dto.setOrderNo(order.getOrderNo());
            dto.setTotalAmount(order.getTotalAmount());
            dto.setActualAmount(order.getActualAmount());
            dto.setOrderStatus(order.getStatus());
            dto.setPaymentType(order.getPaymentType());
            dto.setNickname(order.getNickname());
            dto.setItemCount(order.getItemCount());
            
            // 设置地址信息对象
            if (order.getAddressInfo() != null) {
                // 直接从order中复制完整的地址信息
                dto.setAddressInfo(order.getAddressInfo());
                
                // 从订单地址获取送达地址
                dto.setDeliveryAddress(order.getAddressInfo().getDetailedAddress());
                
                // 获取联系人电话
                dto.setPhone(order.getAddressInfo().getReceiverPhone());
            }
            
            if (order.getOrderItems() != null && !order.getOrderItems().isEmpty()) {
                dto.setItemNames(order.getOrderItems().stream()
                        .map(OrderItemDTO::getCommodityName)
                        .collect(Collectors.toList()));
            }
        }
        
        return dto;
    }
} 